<?php
/**
 * Created by JetBrains PhpStorm.
 * User: T420
 * Date: 9/27/12
 * Time: 10:16 PM
 * To change this template use File | Settings | File Templates.
 */
class Models_Product extends Libs_Model
{
    public $totalpage;
    public function __construct()
    {
        parent::__construct();
    }

    public function getproductbyfc($id,$pagenum,$productperpage)
    {
        $this->db->connect();
        $str="select * from lb_product where pfid='$id'";
        $this->totalpage=count($this->db->executeQuery($str));
        if(!$pagenum || $pagenum<=0 || $pagenum>$this->totalpage)
            $pagenum=1;
        if($pagenum==1)
            $from=0;
        else
            $from=($pagenum-1)*$productperpage;
        $str.="order by pid DESC limit $from,$productperpage";
        $rs=$this->db->executeQuery($str);
        $this->db->disconnect();
        return $rs;
    }
    public function getproductbymenu($id,$pagenum,$productperpage)
    {
        $this->db->connect();
        $str="select * from lb_product a, lb_menuproduct b, lb_menusystem c where a.pfid=b.pfid and b.menuid=c.menuid and c.menuid='$id'";
        $this->totalpage=count($this->db->executeQuery($str));
        if(!$pagenum || $pagenum<=0 || $pagenum>$this->totalpage)
            $pagenum=1;
        if($pagenum==1)
            $from=0;
        else
            $from=($pagenum-1)*$productperpage;
        $str.="order by pid DESC limit $from,$productperpage";
        $rs=$this->db->executeQuery($str);
        $this->db->disconnect();
        return $rs;
    }
    public function getmenupro($id)
    {
        $this->db->connect();
        $list=$this->db->executeQuery("select * from lb_menusystem a, lb_menuproduct b where a.menuid=b.menuid and pfid='$id'");
        $this->db->disconnect();
        return $list;
    }
    public function getmenuviewall($id)
    {
        $this->db->connect();
        $list=$this->db->executeQuery("select * from lb_menusystem where menuid='$id'");
        $this->db->disconnect();
        return $list;
    }
    public function getDetailProduct($id)
    {
        $this->db->connect();
        $listPro=$this->db->executeQuery("select * from lb_product where pid='$id'");
        $this->db->disconnect();
        return $listPro;
    }
    public function searchproduct($pname,$pfid,$mfid,$pagenum,$productperpage)
    {
        $this->db->connect();
        $str="select * from lb_product ";
        if($pname!='')
        {
            $str.="where pname like '%$pname%'";
            if($pfid!=0) $str.="and pfid='$pfid'";
            if($mfid!=0) $str.="and mfid='$mfid'";

        }

        $this->totalpage=count($this->db->executeQuery($str));
        if(!$pagenum || $pagenum<=0 || $pagenum>$this->totalpage)
            $pagenum=1;
        if($pagenum==1)
            $from=0;
        else
            $from=($pagenum-1)*$productperpage;
        $str.="order by pid DESC limit $from,$productperpage";
        $rs=$this->db->executeQuery($str);
        $this->db->disconnect();

        return $rs;

    }

}
